import { api } from './api';

// 用户登录
export const loginUser = async (email: string, password: string) => {
  const response = await api.post('/api/auth/login', { email, password });
  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.message || 'Login failed');
  }
  return response.json();
};

// 用户注册
export const registerUser = async (username: string, email: string, password: string) => {
  const response = await api.post('/api/auth/register', { username, email, password });
  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.message || 'Registration failed');
  }
  return response.json();
};

// 用户登出
export const logoutUser = async () => {
  const response = await api.post('/api/auth/logout', {});
  return response.json();
};

// 获取当前用户信息
export const getCurrentUser = async () => {
  const response = await api.get('/api/auth/me');
  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.message || 'Failed to get user info');
  }
  return response.json();
};

// 更新用户信息
export const updateUserProfile = async (userData: any) => {
  const response = await api.put('/api/auth/profile', userData);
  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.message || 'Failed to update profile');
  }
  return response.json();
};